# -*- python -*-
# ex: set syntax=python:

# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import config
import master_site_config

from buildbot.scheduler import Periodic
from buildbot.schedulers.timed import Nightly
from buildbot.status import mail

from master import exception_notifier
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory


ActiveMaster = master_site_config.InfraCron

c = BuildmasterConfig = {}
c['slavePortnum'] = ActiveMaster.slave_port
c['schedulers'] = []
c['builders'] = []
c['change_source'] = []
c['status'] = []

def mergeRequests(req1, req2):
  if 'publish_tarball' in (req1.buildername, req2.buildername):
    return False
  return req1.canBeMergedWith(req2)
c['mergeRequests'] = mergeRequests

f_annotations = annotator_factory.AnnotatorFactory(ActiveMaster)

####### DATABASE

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### BUILDERS

c['builders'].extend([
  {
    'name': 'gnumbd',
    'factory': f_annotations.BaseFactory(
        'infra/gnumbd',
        {'target_repo': 'https://chromium.googlesource.com/chromium/src'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gnumbd-v8',
    'factory': f_annotations.BaseFactory(
        'infra/gnumbd',
        {'target_repo': 'https://chromium.googlesource.com/v8/v8'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gnumbd-webrtc',
    'factory': f_annotations.BaseFactory(
        'infra/gnumbd',
        {'target_repo': 'https://chromium.googlesource.com/external/webrtc'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/chromium/src'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-luci-py',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://github.com/luci/luci-py'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-mojo',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://github.com/domokit/mojo'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-webrtc',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/external/webrtc'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'lkgr-tag-pusher',
    'factory': f_annotations.BaseFactory('infra/lkgr_tag_pusher'),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'builder-alerts',
    'factory': f_annotations.BaseFactory('infra/builder_alerts'),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'publish_tarball',
    'factory': f_annotations.BaseFactory('infra/publish_tarball'),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'Chromium Linux Codesearch',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': '2codesearch',
  },
  {
    'name': 'ChromiumOS Codesearch',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': '2codesearch',
  },
  {
    'name': 'Chromium Linux Codesearch Staging',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': '2codesearch',
  },
  {
    'name': 'ChromiumOS Codesearch Staging',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': '2codesearch',
  },
])

####### SCHEDULERS

c['schedulers'].extend([
  # Period is intentionally small so that if a builder fails fast, it also
  # gets restarted fast.
  Periodic(
      name='infra-runner-scheduler',
      periodicBuildTimer=1*60,
      builderNames=[
          'gnumbd',
          'gnumbd-v8',
          'gnumbd-webrtc',
          'gsubtreed',
          'gsubtreed-luci-py',
          'gsubtreed-mojo',
          'gsubtreed-webrtc',
          'lkgr-tag-pusher',
          'builder-alerts',
      ]
  ),
  Nightly(
      name='publish_tarball',
      branch=None,
      builderNames=['publish_tarball'],
      hour=[12]
  ),
  Nightly(
      name='codesearch-scheduler',
      branch='master',
      hour=range(0, 24, 4),
      minute=0,
      builderNames=['Chromium Linux Codesearch', 'ChromiumOS Codesearch']),
  Nightly(
      name='codesearch-staging-scheduler',
      branch='master',
      hour=range(2, 24, 4),
      minute=0,
      builderNames=['Chromium Linux Codesearch Staging',
                    'ChromiumOS Codesearch Staging']),
])

####### BUILDSLAVES

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'InfraCron')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(
    c['builders'], config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

####### STATUS TARGETS

# Adds common status and tools (web status, mail notifier) to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster, mail_notifier=False, order_console_by_time=True,
    public_html='../master.chromium/public_html',
    templates=['templates', '../master.chromium/templates']
)

# Send alerts when builders fail.
c['status'].extend([
  exception_notifier.ExceptionNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['gnumbd'],
      subject='gnumbd failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+gnumbd@google.com'],
  ),
  exception_notifier.ExceptionNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['gnumbd-v8'],
      subject='gnumbd failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+gnumbd@google.com',
                       'machenbach@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['gnumbd-webrtc'],
      subject='gnumbd-webrtc failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+gnumbd@google.com',
                       'kjellander@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['gsubtreed-mojo'],
      subject='gsubtreed-mojo failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+gsubtreed-mojo@google.com',
                       'mojo-mirror-infra@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['lkgr-tag-pusher'],
      subject='lkgr-tag-pusher failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+lkgr-tag-pusher@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['builder-alerts'],
      subject='builder-alerts failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+builder-alerts@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['Chromium Linux Codesearch', 'ChromiumOS Codesearch'],
      subject='Codesearch build failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['chrome-troopers+codesearch-alerts@google.com'],
  ),
])


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['buildbotURL'] = ActiveMaster.buildbot_url
